<template>
  <div class="app-container">
    <el-table
      v-loading="listLoading"
      :data="data"
      row-key="id"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      stripe
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >

      <el-table-column
        type="index"
        width="50"
      />

      <el-table-column
        align="center"
        prop="id"
        label="订单编号"
      />
      <el-table-column
        align="center"
        prop="code"
        label="商品编号"
      />
      <el-table-column
        prop="name"
        label="名称"
        width="120"
        align="center"
      />

      <el-table-column
        prop="price"
        label="价格"
        width="150"
        align="center"
      />

      <el-table-column
        prop="dateCreated"
        label="创建时间"
        width="120"
        align="center"
      />

      <el-table-column
        prop="dateModified"
        label="修改时间"
        width="120"
        align="center"
      />

      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" icon="el-icon-edit">修改</el-button>

          <el-button type="danger" size="mini" icon="el-icon-delete" @click="removeDataById(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>

    </el-table>
    <el-pagination
      :current-page="page"
      :page-size="limit"
      :total="total"
      background
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="fetchData"
    />

  </div>
</template>
<style>
.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}
</style>

<script>
export default {
  data() {
    return {
      listLoading: false,
      list: null,
      page: 1,
      limit: 10,
      total: 0,
      data: [{
        id: '0000001',
        code: '0000002',
        price: '1',
        dateModified: '2016-05-02',
        dateCreated: '2016-05-02'
      },
      {
        id: '0000002',
        code: '0000007',
        price: '3',
        dateModified: '2016-05-02',
        dateCreated: '2016-05-02'
      },
      {
        id: '0000003',
        code: '0000005',
        price: '2.5',
        dateModified: '2016-05-02',
        dateCreated: '2016-05-02'
      },
      {
        id: '0000004',
        code: '0000002',
        price: '4.46',
        dateModified: '2016-05-02',
        dateCreated: '2016-05-02'
      },
      {
        id: '0000005',
        code: '0000006',
        price: '20',
        dateModified: '2016-05-02',
        dateCreated: '2016-05-02'
      },
      {
        id: '0000006',
        code: '0000001',
        price: '10.5',
        dateModified: '2016-05-02',
        dateCreated: '2016-05-02'
      }
      ]
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    removeDataById(v) {
      this.data = this.data.filter(({ id }) => {
        // eslint-disable-next-line eqeqeq
        return id != v
      })
    },
    fetchData(page = 1) {
      this.page = page
    }
  },
  tableRowClassName({ row, rowIndex }) {
    if (rowIndex === 1) {
      return 'warning-row'
    } else if (rowIndex === 3) {
      return 'success-row'
    }
    return ''
  }
}
</script>
